<script type="text/javascript" src="http://api.map.baidu.com/getscript?v=2.0&ak=DpM2LLeXhxIaS6jMmIGcCYo93rEvtRAV"></script>
<div class="map_div" style="display: none">
    <div id="l-map"></div>
    <img style="position: absolute; left: 50%;top: 50%;z-index: 999;" src="<?php echo $this->staticUrl;?>img/map.png" alt="">
    <div class="vCenter bag1 map_div2">
        <img class="mr10 back" src="<?php echo $this->staticUrl;?>img/png/pic138.png" alt="">
        <input type="text" id="suggestId" size="20" class="form-control required wp80 b0" placeholder="请输入地址"  />
        <a href="javascript:;" class="wp20 tc bag2 cor1 jdjz qd">确定</a>
    </div>
    <div id="searchResultPanel" style="border:1px solid #C0C0C0;width:150px;height:auto; display:none;"></div>
    <input type="hidden" id="addr"/>
    <input type="hidden" id="lat"/>
    <input type="hidden" id="lng"/>
    <script type="text/javascript">
        // 百度地图API功能
        function G(id) {
            return document.getElementById(id);
        }
        var lat = '<?php echo  $this->latitude ?>';
        var lng = '<?php echo  $this->longitude ?>';
        var pt;
        if(lat != 0 && lng != 0){
             pt = new BMap.Point(<?php echo  $this->longitude ?>,<?php echo  $this->latitude ?>);//店铺坐标
        }else{
             pt = '庐阳区';
        }

        var map = new BMap.Map("l-map");
        map.centerAndZoom(pt,16);                   // 初始化地图,设置城市和地图级别。

        var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
            {"input" : "suggestId"
                ,"location" : map
            });
        map.enableScrollWheelZoom(true); // 缩放地图

        // 中心位置移动
        map.addEventListener("dragend", function(){
            var center = map.getCenter();
            var geoc = new BMap.Geocoder();
            geoc.getLocation(center, function(rs){
                if (rs.surroundingPois.length){
                    var title = rs.surroundingPois[0];
                    $("#addr").val(title.title);
                    $("#suggestId").val(title.title);
                    $("#lat").val(title.point.lat);
                    $("#lng").val(title.point.lng);
                }else{
                    $("#addr").val(rs.address);
                    $("#suggestId").val(rs.address);
                    $("#lat").val(center.lat);
                    $("#lng").val(center.lng);
                }
            });
        });

        ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
            var str = "";
            var _value = e.fromitem.value;
            var value = "";
            if (e.fromitem.index > -1) {
                value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
            }

            str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

            value = "";
            if (e.toitem.index > -1) {
                _value = e.toitem.value;
                value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
            }
            $("#addr").val(value);
            str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
            G("searchResultPanel").innerHTML = str;
        });

        var myValue;
        ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
            var _value = e.item.value;
            myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
            G("searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;
            setPlace();
        });

        function setPlace(){
            map.clearOverlays();    //清除地图上所有覆盖物
            function myFun(){
                var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
                $("#lat").val(pp.lat);
                $("#lng").val(pp.lng);
                map.centerAndZoom(pp, 18);
//                        map.addOverlay(new BMap.Marker(pp));    //添加标注
            }
            var local = new BMap.LocalSearch(map, { //智能搜索
                onSearchComplete: myFun
            });
            local.search(myValue);
        }

        $(function () {
            // 关闭地图
            $(".qd").click(function () {
                var addr = $("#addr").val();
                var lat = $("#lat").val();
                var lng = $("#lng").val();
                if(!addr.length || !lat.length || !lng.length){
                    show_message({status:0, message:'请输入正确的地址'});
                    return false;
                }
                $(".address").html(addr);
                $(".address").val(addr);
                $("[name='address']").val(addr);
                $("[name='latitude']").val(lat);
                $("[name='longitude']").val(lng);
                $(".map_div").hide();
                $(".content").show();
            });

            $(".back").click(function () {
                $(".map_div").hide();
                $(".content").show();
            });
        })
    </script>
</div>